/*
 * Project it.battlehorse.rcp.tools.actors.sample
 * Created on Nov 19, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */
package it.battlehorse.rcp.tools.actors.sample;

import it.battlehorse.rcp.tools.actors.IActorRunnable;
import it.battlehorse.rcp.tools.commons.actors.IActorExecutionStatus;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;

/**
 * Defines a sample job which counts seconds.
 * 
 * @author battlehorse
 * @since Nov 19, 2005
 */
class SampleJob implements IActorRunnable {
	
	private int jobLength;
	
	private boolean canceled = false;
	private int counted = 0;
	
	public SampleJob(SampleActorView sv) {					
		jobLength = Integer.parseInt(sv.getSeconds());			
	}
	
	public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		monitor.beginTask("Calculating",jobLength);
		
		for (int i = 0; i < jobLength; i++) {
			System.out.println("Elapsed " + i + " seconds");
			if (i == 10)
				monitor.subTask("Sample subtask starting ...");
			
			try {
				Thread.sleep(1000);
				monitor.worked(1);
			}
			catch(Exception e) {}
			
			counted++;
			
			if (monitor.isCanceled()) {
				monitor.done();
				canceled = true;
				return;
			}
				
		}
		
		monitor.done();
		
		return;
	}
	
	public IActorExecutionStatus getExecutionStatus() {
		
		return new SampleExecutionStatus(counted,canceled);
	}
}